<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Author: ThanhTV
* Email: thanhtran@vietnambiz.com
* Date create: Web,24/08/2011
* Description: Products controller
*/
require_once('admin_controller'.EXT);

class products extends admin_controller
{
    var $data = array();

    function products()
    {
        $this->load->library('pagination');
		parent::admin_controller();
		$this->load->model(array("products_model","product_type_model","company_model"));  
		$this->data['product_type']	= $this->product_type_model->get_all_product_type(1,50);
    	$this->data['company']		= $this->company_model->get_all_company(1,50);      
    }

    function index()
    {    	    
        $this->data["all_products"] = $this->products_model->get_all_products(1,20);
        $this->data['tpl_view'] = 'products/index';
        $this->load->view('admin_layout',$this->data);
    }

    function list_ajax($page = 1)
    {
        //pagination
        $limit 		= ($this->input->post('limit') > 0) ? $this->input->post('limit') : 20;
        $num_rows 	= $this->products_model->get_count_all_products();
        $total 		= ceil($num_rows/$limit);
        $url 		= base_url_admin('products/list_ajax');
        $this->session->set_userdata('url_list_products',$url . '/' . $page);
        $conf = array(
            'cur_page'          =>	$page,
            'total_rows'		=>	$total,
            'base_url'          =>  $url,
            'js_function'		=>	'list_ajax'
        );
        $this->pagination->initialize($conf);
        $this->data['pagnav'] = $this->pagination->ajax_url();
        $this->data['limit']  = $limit;
        //end pagination

        $this->data["all_products"] = $this->products_model->get_all_products($page,$limit);
        
        $this->load->view('products/list_ajax',$this->data);
    }

    function add()
    {    	    	    	
        if($_SERVER["REQUEST_METHOD"]=="POST")
        {
        	$this->form_validation->set_rules('name', 'name', 'required');
            if($this->form_validation->run() == TRUE)
            {
	            $name   				= $this->input->post('name');
	            $code					= $this->input->post('code');
	            $id_company				= $this->input->post('id_company');
	            $id_product_type		= $this->input->post('id_product_type');
	            $warranty				= $this->input->post('warranty');
	            $country				= $this->input->post('country');
	            $description			= $this->input->post('description');
	            $electric_power			= $this->input->post('electric_power');
	            $cool_power				= $this->input->post('cool_power');
	            $size					= $this->input->post('size');
	            $cool_weight			= $this->input->post('cool_weight');
	            $hot_weight				= $this->input->post('hot_weight');
	            $cool_size				= $this->input->post('cool_size');
	            $hot_size				= $this->input->post('hot_size');
	            $power_consumption		= $this->input->post('power_consumption');
	            $absorb_moisture		= $this->input->post('absorb_moisture');
	            $features				= $this->input->post('features');
	            $price					= $this->input->post('price');
	            $status					= $this->input->post('status');
	            
	            if($this->products_model->check_code_products($code)==FALSE)
	            {                
	                $arr_in = array(
	                    'name'                 	=> $name,
	                	'name_link'            	=> create_link($name),
	                	'code'					=> $code,
	                	'id_company'            => $id_company,
	                	'warranty'				=> $warranty,
	                	'country'				=> $country,
	                	'description'			=> $description,
	                	'id_product_type'		=> $id_product_type,
	                	'electric_power'		=> $electric_power,
	                	'cool_power'			=> $cool_power,
	                	'size'					=> $size,
	                	'cool_weight'			=> $cool_weight,
	                	'hot_weight'			=> $hot_weight,
	                	'cool_size'				=> $cool_size,
	                	'hot_size'				=> $hot_size,
	                	'power_consumption'		=> $power_consumption,
	                	'absorb_moisture'		=> $absorb_moisture,
	                	'features'				=> $features,
	                	'price'					=> $price,	                		                
	                    'status'               	=> $this->input->post('status')
	                );
	    	print_r($arr_in);die();
	                $re = $this->db->insert('products', $arr_in);
	            
	                if(!$re)
	                    {die("not_ok");}
	                    else{die("ok");}
	             }
	             else{
	                    die("name_exits");
	                 }
            }

        }
        $this->load->view('products/add',$this->data);
    }

    function edit()
    {
        $act = $this->input->post('act');
        $id  = $this->input->post("id");
        if($act == 'process')
        {
            $name   	= $this->input->post('name');
			
            if($this->questions_subjects_model->check_title_edit($name,$id)==FALSE)
            {            
               $arr_in = array(
                    'name'                 => $name,
                	'name_link'            => create_link($name),                	                                       
                    'status'               => $this->input->post('status')
                );

                $re = $this->db->where('id', $id)->update('questions_subjects', $arr_in);
                die("ok");
            }
            else{ die("name_exits"); }
        }
        $this->data["questions_subjects_info"] = $this->questions_subjects_model->get_questions_subjects_info($id);
        $this->load->view('questions_subjects/edit',$this->data);
    }

    function change_status()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST'){

            $active  = $this->input->post('mode');
            $id = $this->input->post('id');

            $this->db->where_in('id', $id);
            $this->db->update('questions_subjects', array('status' => $active));

            die($active);
        }
    }   

    function change_multi_status()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $active  = $this->input->post('sel_action');
            $id = $this->input->post('id');
            $this->db->where_in('id', $id);
            $this->db->update('questions_subjects', array('status' => $active));

            die($active);
        }
    }

    function del($id)
    {
        if(!$this->questions_subjects_model->del_questions_subjects($id))
        {die('no');}
        die('yes');
    }

    function change_order()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $cats = $this->input->post('lib');

            foreach($cats as $cat)
            {
                if($cat['old_order'] != $cat['order'])
                {
                    $this->questions_subjects_model->change_order($cat['id'], $cat['old_order'], $cat['order']);
                }
            }
            die(1);
        }
    }

    function view($id=0)
    {
        $this->data["questions_subjects_info"] = $this->questions_subjects_model->get_questions_subjects_info($id);
        $this->load->view('questions_subjects/view',$this->data);
    }
}